# -*- coding: utf-8 -*-
"""
@Time    : 2021/12/2 15:43
@Author  : jins
@File    : priority.py
"""
from multiprocessing import Process
from threading import Thread
import common.requests_api as r

url = '10.0.17.17:8529/iot/webCallBusiness/v1'
timeout = 300
body_1 = {
    "requestId": "123456",
    "sysCode": "101",
    "wsCode": "",
    "token": "8014d2d8a85449078be70cd79b9bf389_os_web",
    "requestParams": [
        {
            "businessCode": "device_clock_param_call",
            "schemaCode": "",
            "deviceCodes": {
                "333300001": [
                    2
                ],
                "master": [
                    "333300001"
                ]
            },
            "params": {},
            "properties": {
                "TAT": "0",
                "MSA": "10"
            }
        }
    ],
    "requestTime": "",
}

body_2 = {
    "requestId": "123456",
    "sysCode": "101",
    "wsCode": "",
    "token": "8014d2d8a85449078be70cd79b9bf389_os_web",
    "requestParams": [
        {
            "businessCode": "device_clock_param_call",
            "schemaCode": "",
            "deviceCodes": {
                "333300001": [
                    2
                ],
                "master": [
                    "333300001"
                ]
            },
            "params": {},
            "properties": {
                "TAT": "0",
                "MSA": "10"
            }
        }
    ],
    "requestTime": "",
    "priority": 2
}

body_list = [body_1, body_2]


# res = r.auto_request(url=url, body=body_2, timeout=timeout)
# print(res['data'][0]['deviceResults'][0]['data'][0]['txtdate_tim'])
def priority_demo(body, file_path):
    # for i in range(30):
    res = r.auto_request(url=url, body=body, timeout=timeout)
    with open(file_path, 'a') as f:
        # f.write(res['data'][0]['deviceResults'][0]['data'][0]['txtdate_tim'] + '\n')
        f.write(str(res) + '\n')


if __name__ == '__main__':
    process_list = []
    for i in range(5):
        body_1['requestId'] = i + 1
        p = Process(target=priority_demo, args=(body_1, '1.txt'))
        p.start()
        process_list.append(p)
    for i in range(5, 10):
        body_2['requestId'] = i + 1
        p = Process(target=priority_demo, args=(body_2, '2.txt'))
        p.start()
        process_list.append(p)
    for i in process_list:
        i.join()
